<template>
  <div class="user-info-compontents">
    <el-form ref="ruleForm" :rules="rules" :model="params" label-width="5rem">
      <div class="base-info">
        <div class="icon-upload">
          <label><span>*</span> 头像：</label>
          <upload ref="upload" :limit="1" :file-list="fileList" :disabled="disabled" :file-filter="filterImgType" :name-show="false" />
        </div>
        <div class="basics-info">
          <el-row :gutter="40">
            <el-col :span="24">
              <el-form-item label="账号">
                <label>+86-{{ params.mobile }}</label>
                <label v-if="!disabled" class="blueLink" @click="updatePwd()">修改密码</label>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item prop="name" label="姓名">
                <el-input v-model="params.name" :disabled="disabled" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="sex" label="性别">
                <div class="fromFlex">
                  <el-radio v-model="params.sex" :label="1" :disabled="disabled">男</el-radio>
                  <el-radio v-model="params.sex" :label="2" :disabled="disabled">女</el-radio>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="demoUser" label="演示账号">
                <div class="fromFlex">
                  <el-radio v-model="params.demoUser" :label="0" :disabled="disabled">否</el-radio>
                  <el-radio v-model="params.demoUser" :label="1" :disabled="disabled">是</el-radio>
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item prop="nickName" label="昵称">
                <el-input v-model="params.nickName" :disabled="disabled" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item prop="email" label="邮箱">
                <el-input v-model="params.email" :disabled="disabled" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="个人签名">
                <el-input v-model="params.description" :disabled="disabled" type="textarea" :rows="3" placeholder="最多200字" maxlength="200" />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </div>
    </el-form>
    <div class="btnList">
      <el-button v-if="!disabled" type="primary" class="submitBtn" @click="submit">确认修改</el-button>
      <el-button @click="close">取消</el-button>
    </div>
  </div>
</template>

<script>
import upload from '@/components/upload/uploadAvatar'
import { modifySelf, getUserInfo } from '@/api/user'
export default {
  components: {
    upload
  },
  props: {
    disabled: {
      type: Boolean,
      required: false,
      default: false
    }
  },
  data() {
    return {
      fileList: [],
      params: {},
      filterImgType: ['jpg', 'jpeg', 'png', 'icon', 'gif', 'bmp', 'JPG', 'JPEG', 'PNG', 'ICON', 'GIF', 'BMP'],
      rules: {
        tel: [
          { required: true, message: '请填写昵称', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请填写姓名', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请填写姓名', trigger: 'change' }
        ],
        demoUser: [
          { required: true, message: '请填写演示账号', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getInfo()
  },
  methods: {
    submit() {
      if (this.$demoUser.checkUser()) {
        alert('演示账号，不能执行此操作')
        return
      }
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if (this.$refs.upload.key && this.$refs.upload.key.length > 0) {
            this.params.image = this.$refs.upload.key[0].urlName
          } else {
            this.$message.error('图片不能为空')
            return
          }
          console.log('111')
          modifySelf(this.params).then(response => {
            this.$message.success('修改成功')
            this.$emit('close')
          })
        }
      })
    },
    getInfo() {
      getUserInfo().then(response => {
        this.params = response
        if (this.params.image) {
          this.fileList = [{
            url: this.params.image
          }]
        }
      })
    },
    close() {
      this.$emit('close')
    },
    loginOut() {
      this.$emit('close')
      // var id = null
      // this.$confirm('此操作将永久删除该账号, 是否继续?', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(() => {
      //   logout({ id }).then(response => {
      //     this.$message.success('注销成功')
      //     this.$router.push({
      //       name: 'login'
      //     })
      //   })
      // })
    },
    updatePwd() {
      if (this.$demoUser.checkUser()) {
        alert('演示账号，不能执行此操作')
        return
      }
      this.$router.push({
        name: 'retrieve-pass'
      })
    }
  }
}
</script>

<style lang="scss">
.user-info-compontents {
  display: flex;

  flex-direction: column;
  margin:0 auto;
  .blueLink {
    color: #409eff;
    font-size: 14px;
    margin-left: 20px;
    cursor: pointer;
  }
  .btnList {
    margin-top:20px;
    display: flex;
    justify-content: center;
    align-items: right;
    .submitBtn {
      margin-right: 30px;
    }
  }
  .fromFlex {
    height: 36px;
    display: flex;
    align-items: center;
  }
  .base-info {
    display: flex;
    margin-bottom: 20px;
    .icon-upload {
      display: flex;
      flex-direction: column;
      align-items: center;
      margin-right: 20px;
      >label {
        margin-bottom: 10px;
        span {
          color: #ff4949;
        }
      }
      .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 5px;
        margin: 0 30px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        .avatar-uploader-icon {
          font-size: 28px;
          color: #8c939d;
          width: 150px;
          height: 150px;
          line-height: 150px;
          text-align: center;
        }
      }
    }
    .basics-info {
      width: 100%;
    }
  }
}
</style>
